package com.nmgydy.admin.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nmgydy.beans.ListBean;
import com.nmgydy.beans.Message;
import com.nmgydy.beans.ResultBean;
import com.nmgydy.model.Admin;
import com.nmgydy.model.EventItem;
import com.nmgydy.model.UserConfirm;
import com.nmgydy.service.EventItemService;
import com.nmgydy.util.Functions;
import com.nmgydy.util.HibernateDao;
import com.nmgydy.util.QrcodeUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.font.FontDesignMetrics;

@Controller
@RequestMapping("/admin/eventItem")
public class EventItemController {

	@Resource
	private HibernateDao hibernateDao;

	@Resource
	private EventItemService eventItemService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(String status,HttpServletRequest request, HttpServletResponse response, Model model) {
		Admin admin = (Admin) request.getSession().getAttribute("admin");
		model.addAttribute("roleIds",admin.getRoleIds());
		model.addAttribute("status",status);
		return "/admin/eventItem/list";
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "/admin/eventItem/add";
	}

	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public String edit(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/edit";
	}
	@RequestMapping(value = "preview", method = RequestMethod.GET)
	public String preview(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/preview";
	}
	@RequestMapping(value = "audit", method = RequestMethod.GET)
	public String audit(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/audit";
	}
	
	@RequestMapping(value = "search", method = RequestMethod.GET)
	public String search(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/search";
	}
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	public String detail(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/detail";
	}
	@RequestMapping(value = "user_confirm_list", method = RequestMethod.GET)
	public String userConfirmList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/user_confirm_list";
	}
	@RequestMapping(value = "team_confirm_list", method = RequestMethod.GET)
	public String teamConfirmList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "/admin/eventItem/team_confirm_list";
	}

	@RequestMapping(value = "listData", method = RequestMethod.POST)
	@ResponseBody
	public String listData(String status,HttpServletRequest request, HttpServletResponse response, Model model) {
		String pageSize = request.getParameter("pageSize");
		String pageIndex = request.getParameter("pageIndex");
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");

		String hql = "from EventItem a where a.id=a.id ";
		Map<String, Object> params = new HashMap<String, Object>();

		if(!Functions.isEmpty(status)){
			hql += " and a.status=:status";
			params.put("status", status);
		}

		Admin admin = (Admin) request.getSession().getAttribute("admin");
		if(admin.getRoleIds().contains("2")){
			hql += " and a.userId=:userId";
			params.put("userId", admin.getId());
		}
		
		String title = request.getParameter("title");
		if (!Functions.isEmpty(title)) {
			hql += " and title like :title";
			params.put("title", "%" + title + "%");
		}
		String lipic = request.getParameter("lipic");
		if (!Functions.isEmpty(lipic)) {
			hql += " and lipic like :lipic";
			params.put("lipic", "%" + lipic + "%");
		}
		String startTime = request.getParameter("startTime");
		if (!Functions.isEmpty(startTime)) {
			hql += " and startTime like :startTime";
			params.put("startTime", "%" + startTime + "%");
		}
		String address = request.getParameter("address");
		if (!Functions.isEmpty(address)) {
			hql += " and address like :address";
			params.put("address", "%" + address + "%");
		}
		String content = request.getParameter("content");
		if (!Functions.isEmpty(content)) {
			hql += " and content like :content";
			params.put("content", "%" + content + "%");
		}
		Boolean sorted = !(sortField == null || "".equals(sortField) || sortOrder == null || "".equals(sortOrder));
		if (sorted) {
			hql += " order by :sortField :sortOrder";
			params.put("sortField", sortField);
			params.put("sortOrder", sortOrder);
		}else{
			hql += " order by id desc";
		}
		
		if (!Functions.isEmpty(pageIndex) && !Functions.isEmpty(pageSize)) {
			long total = hibernateDao.count(hql, params);
			List<?> list = hibernateDao.list(hql, params, Integer.parseInt(pageIndex) + 1, Integer.parseInt(pageSize));
			return JSON.toJSONString(new ListBean(list,total));
		} else {
			long total = hibernateDao.count(hql, params);
			List<?> list = hibernateDao.list(hql, params);
			return JSON.toJSONString(new ListBean(list,total));
		}
	}

	
	//编辑
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(EventItem eventItem,HttpServletRequest request, HttpServletResponse response, Model model) {
		if(eventItem.getId() == null){
			return JSON.toJSONString(ResultBean.failure("id is null"));
		}
		if(Functions.isEmpty(eventItem.getMessage())){
			return JSON.toJSONString(ResultBean.failure("message is null"));
		}
		String hql = "update EventItem set message=:message,status='2' where id=:id";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("message",eventItem.getMessage());
		params.put("id",eventItem.getId());
		hibernateDao.executeUpdate(hql,params);

		return JSON.toJSONString(ResultBean.success("success"));
	}

	@RequestMapping(value = "updateAudit", method = RequestMethod.POST)
	@ResponseBody
	public String updateAudit(EventItem eventItem,HttpServletRequest request, HttpServletResponse response, Model model) {
		if(eventItem.getId() == null){
			return JSON.toJSONString(ResultBean.failure("id is null"));
		}
		if(Functions.isEmpty(eventItem.getStatus())){
			return JSON.toJSONString(ResultBean.failure("status is null"));
		}
		String hql = "update EventItem set status=:status where id=:id";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("status",eventItem.getStatus());
		params.put("id",eventItem.getId());
		hibernateDao.executeUpdate(hql,params);

		return JSON.toJSONString(ResultBean.success("success"));
	}

	@RequestMapping(value = "overToday", method = RequestMethod.POST)
	@ResponseBody
	public String overToday(HttpServletRequest request, HttpServletResponse response, Model model) {
		eventItemService.overToday();
		return JSON.toJSONString(ResultBean.success("success"));
	}

}
